<%@ page language="java" import="java.util.*,org.apache.commons.fileupload.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>商品上架</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<style>
		body>p{font-size:16px;position:absolute;top:193px;left:350px;}
		#pk{border:1px solid #ccc;width:550px;margin-top:-520px;
		margin-bottom:70px;padding-bottom: 20px;margin-left:470px;}
		#lb input[type=text],#lb select{box-shadow:inset 1px 1px 2px #ccc;
		border:1px solid #999;border-radius:3px;height:26px;width:220px;
		margin-left:20px;}
		#lb tr{line-height:36px;}
		#des{margin-left:20px;resize:none;width:220px;border-radius:3px;height:40px;}
		#pimg{margin-left:20px;width:60px;resize:none;height:50px;margin-top:5px;
		margin-bottom:6px;border:1px solid #999;border-radius:3px;}
		input[type=file]{margin-left:19px;}
		#img{width:100%;height:100%;}
		.btn{width:90px;height:36px;margin-top:20px;color:#fff;background-color:#FF4E00;
		border:none;border-radius:5px;font-size:16px;font-weight: bold;}
		#sub{margin-left:25px;margin-right:75px;}
		#mess{height:26px;}
		#lb tr:FIRST-CHILD {line-height:24px;color:#f00;}
		#wq{width:500px;height:300px;margin:-480px 0 160px 450px;}
		#wq>img{margin-left:196px;margin-top:56px;}
		#wq>p{font-size:16px;text-align:center;}
		#wq a{text-decoration: underline;}
	</style>
	<script type="text/javascript" src="js/jquery-1.8.2.min.js"></script>
	<script type="text/javascript">
		$(function(){
			findLevelOne();
			findLevelTwo();
			findLevelThree();
			$("#one").change(function(){
				findLevelTwo();
				findLevelThree();
			});
			$("#two").change(function(){
				findLevelThree();
			});
			$("#file").change(function(){
				getPath(document.getElementById("file"));
			});
			$("#add").submit(function(){
				var reg = /^\d*$/;
				if($("#name").val()==""||$("#price").val()==""||$("#stock").val()==""
				||$("#one").val()==null||$("#two").val()==null||$("#three").val()==null
				||$("#des").val()==""||$("#file").val()==""){
				$("#mess").html("信息输入不完整！");
				return false;
				}else if(isNaN($("#price").val())){
				$("#mess").html("价格只能为数字！");
				return false;
				}else if($("#price").val()<0){
				$("#mess").html("价格不能小于0");
				return false;
				}else if(!reg.test($("#stock").val())){
				$("#mess").html("库存只能为整数！");
				return false;
				}else if(isNaN($("#jf").val())){
				$("#mess").html("积分只能为数字");
				return false;
				}else if($("#jf").val()<0){
				$("#mess").html("积分不能小于0");
				return false;
				}else{
					$("#mess").html("");
					return true;
				}
			});
			$("#res").click(function(){
				$("#mess").html("");
				$("#img").attr("src","");
			});
		});
		//读取图片本地完整路径
		function getPath(fileDom) {  
   			//判断是否支持FileReader  
   			if (window.FileReader) {  
       			var reader = new FileReader();  
   			} else {  
       			alert("您的设备不支持图片预览功能，如需该功能请升级您的设备！");  
   			}  
   			//获取文件  
		    var file = fileDom.files[0];  
		    var imageType = /^image\//;  
		    //是否是图片  
		    if (!imageType.test(file.type)) {  
		        alert("仅支持图片！");  
		        return;  
		    }  
		    //读取完成  
		    reader.onload = function (e) {  
		        //获取图片dom  
		        var img = document.getElementById("img");  
		        //图片路径设置为读取的图片  
		        img.src = e.target.result;  
		    };  
		    reader.readAsDataURL(file);  
		}  
		function findLevelOne(){
			$.ajax({
				"url":"addProduct",
				"type":"post",
				async:false,
				"data":"opr=findLevelOne",
				"dataType":"json",
				"success":function(data){
					$("#one").empty();
					for(i=0;i<data.length;i++){
						$("#one").append("<option value='"+data[i].id+"'>"+data[i].name+"</option>");
					}
				}
			});
		}
		function findLevelTwo(){
			$.ajax({
				"url":"addProduct",
				"type":"post",
				async:false,
				"data":"opr=findLevelTwo&id="+$("#one").val(),
				"dataType":"json",
				"success":function(data){
					$("#two").empty();
					for(i=0;i<data.length;i++){
						$("#two").append("<option value='"+data[i].id+"'>"+data[i].name+"</option>");
					}
				}
			});
		}
		function findLevelThree(){
			$.ajax({
				"url":"addProduct",
				"type":"post",
				async:false,
				"data":"opr=findLevelThree&id="+$("#two").val(),
				"dataType":"json",
				"success":function(data){
					$("#three").empty();
					for(i=0;i<data.length;i++){
						$("#three").append("<option value='"+data[i].id+"'>"+data[i].name+"</option>");
					}
				}
			});
		}
	</script>
  </head>
  <body>
  <input type="hidden" value="${type}" id="ty"/>
    <%@ include file="topBar.jsp" %>
    <%@ include file="leftBar.jsp" %>
  	<%request.setCharacterEncoding("UTF-8"); %>
  	<c:if test="${type==1}">
    <p>商品上架</p>
	<div id="pk" class="left_m">
		<form action="addProduct?opr=upload" enctype="multipart/form-data" method="post" id="add">
			<table width="355px" id="lb">
				<c:if test="${not empty param.mess }">
					<tr><th colspan="2" id="mess">${param.mess }</th></tr>
				</c:if>
				<c:if test="${empty param.mess }">
					<tr><th colspan="2" id="mess"></th></tr>
				</c:if>
				<tr><td>商品名称</td><td><input type="text" id="name" name="name"/></td></tr>
				<tr><td>商品价格</td><td><input type="text" id="price" name="price"/></td></tr>
				<tr><td>库存数量</td><td><input type="text" id="stock" name="stock"/></td></tr>
				<tr><td>一级分类</td><td>
					<select id="one" name="one">
					</select>
				</td></tr>
				<tr><td>二级分类</td><td>
					<select id="two" name="two">
					</select>
				</td></tr>
				<tr><td>三级分类</td><td>
					<select id="three" name="three">
					</select>
				</td></tr>
				<tr><td>商品描述</td><td><textarea id="des" name="des"></textarea></td></tr>
				<tr><td>商品规格</td><td><input id="gg" type="text" name="gg"/></td></tr>
				<tr><td>商品颜色</td><td><input id="ys" type="text" name="ys"/></td></tr>
				<tr><td>积分</td><td><input id="jf" type="text" name="jf"/></td></tr>
				<tr><td>商品图片</td><td>
				<p id="pimg"><img src="" id="img"/></p>
				<input type="file" id="file" name="nfile" accept="image/*"/></td></tr>
				<tr><td colspan="2"><input type="submit" value="提   交" id="sub" class="btn"/>
				<input type="reset" value="重   置" class="btn" id="res"/></td></tr>
			</table>
		</form>
	</div>    
	 </c:if>
<c:if test="${type!=1}">
	<div id="wq" class="left_m">
    		<img src="images/lock.png"/>
    		<p>您无权查看此页面,<br/>请<a href="Login.jsp">登录</a>到管理员账号查看</p>
    	</div>
	</c:if>
    <%@ include file="bottomBar.jsp" %>
  </body>
</html>
